package log

import (
	"context"
	"fmt"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"io"
)

type ctxZapLog struct {
	log *zap.SugaredLogger
}

func (c *ctxZapLog) Trace(v ...interface{}) {
	c.log.Log(zapcore.DebugLevel, v...)
}

func (c *ctxZapLog) Debug(v ...interface{}) {
	c.log.Log(zapcore.DebugLevel, v...)
}

func (c *ctxZapLog) Info(v ...interface{}) {
	c.log.Log(zapcore.InfoLevel, v...)
}

func (c *ctxZapLog) Notice(v ...interface{}) {
	c.log.Log(zapcore.InfoLevel, v...)
}

func (c *ctxZapLog) Warn(v ...interface{}) {
	c.log.Log(zapcore.WarnLevel, v...)
}

func (c *ctxZapLog) Error(v ...interface{}) {
	c.log.Log(zapcore.ErrorLevel, v...)
}

func (c *ctxZapLog) Fatal(v ...interface{}) {
	c.log.Log(zapcore.FatalLevel, v...)
}

func (c *ctxZapLog) Tracef(format string, v ...interface{}) {
	c.log.Logf(zapcore.DebugLevel, format, v...)
}

func (c *ctxZapLog) Debugf(format string, v ...interface{}) {
	c.log.Logf(zapcore.DebugLevel, format, v...)
}

func (c *ctxZapLog) Infof(format string, v ...interface{}) {
	c.log.Logf(zapcore.InfoLevel, format, v...)
}

func (c *ctxZapLog) Noticef(format string, v ...interface{}) {
	c.log.Logf(zapcore.InfoLevel, format, v...)
}

func (c *ctxZapLog) Warnf(format string, v ...interface{}) {
	c.log.Logf(zapcore.WarnLevel, format, v...)
}

func (c *ctxZapLog) Errorf(format string, v ...interface{}) {
	c.log.Logf(zapcore.ErrorLevel, format, v...)
}

func (c *ctxZapLog) Fatalf(format string, v ...interface{}) {
	c.log.Logf(zapcore.FatalLevel, format, v...)
}

func (c *ctxZapLog) CtxTracef(ctx context.Context, format string, v ...interface{}) {
	if gid, ok := ctx.Value("g-id").(string); ok {
		c.log.Debugw(fmt.Sprintf(format, v...), zap.String("g-id", gid))
	} else {
		c.log.Debugf(format, v...)
	}
}

func (c *ctxZapLog) CtxDebugf(ctx context.Context, format string, v ...interface{}) {
	if gid, ok := ctx.Value("g-id").(string); ok {
		c.log.Debugw(fmt.Sprintf(format, v...), zap.String("g-id", gid))
	} else {
		c.log.Debugf(format, v...)
	}
}

func (c *ctxZapLog) CtxInfof(ctx context.Context, format string, v ...interface{}) {
	if gid, ok := ctx.Value("g-id").(string); ok {
		c.log.Infow(fmt.Sprintf(format, v...), zap.String("g-id", gid))
	} else {
		c.log.Infof(format, v...)
	}
}

func (c *ctxZapLog) CtxNoticef(ctx context.Context, format string, v ...interface{}) {
	if gid, ok := ctx.Value("g-id").(string); ok {
		c.log.Infow(fmt.Sprintf(format, v...), zap.String("g-id", gid))
	} else {
		c.log.Infof(format, v...)
	}
}

func (c *ctxZapLog) CtxWarnf(ctx context.Context, format string, v ...interface{}) {
	if gid, ok := ctx.Value("g-id").(string); ok {
		c.log.Warnw(fmt.Sprintf(format, v...), zap.String("g-id", gid))
	} else {
		c.log.Warnf(format, v...)
	}
}

func (c *ctxZapLog) CtxErrorf(ctx context.Context, format string, v ...interface{}) {
	if gid, ok := ctx.Value("g-id").(string); ok {
		c.log.Errorw(fmt.Sprintf(format, v...), zap.String("g-id", gid))
	} else {
		c.log.Errorf(format, v...)
	}
}

func (c *ctxZapLog) CtxFatalf(ctx context.Context, format string, v ...interface{}) {
	if gid, ok := ctx.Value("g-id").(string); ok {
		c.log.Fatalw(fmt.Sprintf(format, v...), zap.String("g-id", gid))
	} else {
		c.log.Fatalf(format, v...)
	}
}

func (c *ctxZapLog) SetLevel(level hlog.Level) {
}

func (c *ctxZapLog) SetOutput(writer io.Writer) {
}
